// Creo un riferimento al database
function getDatabase() {
    //window.openDatabase(name, version, display_name, size);
    return window.openDatabase("myViaggi", "1.0", "My Viaggi", 200000);
}


// Gestisco il risultati restituiti dal DB
function querySuccess(tx, results) {
    var len = results.rows.length; //restituisce il n. di record presenti nel db
    var output = ""; //stringa di concatenazione
    for(var i=0; i<len; i++) {
        output = output + '<div data-role="collapsible">';
        output = output + '<h3>' + results.rows.item(i).name + '</h3>';
        output = output + '<ul class="objectlist" data-role="listview" data-inset="true">';
        output = output + '<li>Name: ' + results.rows.item(i).name + '</li>';
        output = output + '<li>Type: ' + results.rows.item(i).type + '</li>';
        output = output + '<li>Date: ' + results.rows.item(i).date + '</li>';
        output = output + '<li>Size: ' + results.rows.item(i).size + '</li>';
        output = output + '<li>Latitude: ' + results.rows.item(i).latitude + '</li>';
        output = output + '<li>Longitude: ' + results.rows.item(i).longitude + '</li>';
        output = output + '<li>Full Path: ' + results.rows.item(i).full_path + '</li>';
        output = output + '</ul>';
        output = output + '</div>';
        
        // lista elementi db
        
        $("#audio_list").append("<li data-theme='c' id='"+results.rows.item(i).id+"' title='"+results.rows.item(i).name+"'><img class='ui-li-icon' alt='play this song' src='img/music.png' />" + results.rows.item(i).name + "</li>");
    }
    
    
    $("#audio_list").listview( "refresh" );
    
    
    
    $("#objects").html(output);
}


function querySuccessPoi(tx, results) {
    var len = results.rows.length; //restituisce il n. di record presenti nel db
    var output = ""; //stringa di concatenazione
    for(var i=0; i<len; i++) {
        output = '<li id="'+results.rows.item(i).id+'"><a href="poi/poi_details.html">';
        output = output + results.rows.item(i).name;
        output = output + "</a></li>";
        $("#pois").append(output);
    }
    $("#pois").listview("refresh");

    if(len==0) {
        $("#pois").html("Nessun POI trovato");
    }
}



function detailSuccess(tx, results) {
    var my_item = results.rows.item(0);
    $(".poi_name").html(my_item.name);
    var output = "<p><strong>Name</strong><br />"+my_item.name+"</p>";
    output = output + "<p><strong>Photo</strong><br />"+my_item.photo+"</p>";
    output = output + "<p><strong>Description</strong><br />"+my_item.description+"</p>";
    output = output + "<p><strong>Type</strong><br />"+my_item.type+"</p>";
    output = output + "<p><strong>Address</strong><br />"+my_item.address+"</p>";
    output = output + "<p><strong>Longitude</strong><br />"+my_item.longitude+"</p>";
    output = output + "<p><strong>Latitude</strong><br />"+my_item.latitude+"</p>";
    output = output + "<p><strong>Website</strong><br />"+my_item.web_site+"</p>";
    output = output + "<p><strong>Email</strong><br />"+my_item.email+"</p>";
    output = output + "<p><strong>Phone</strong><br />"+my_item.phone+"</p>";
    output = output + "<p><strong>Fax</strong><br />"+my_item.fax+"</p>";
    output = output + "<p><strong>N. stelle</strong><br />"+my_item.n_stelle+"</p>";
    output = output + "<p><strong>Photogallery</strong><br />"+my_item.photogallery+"</p>";
    output = output + '<p><img width="98%" src="http://maps.googleapis.com/maps/api/staticmap?center='+my_item.latitude+','+my_item.longitude+'&zoom=16&size=500x600&maptype=roadmap&markers=color:green%7Clabel:P%7C'+my_item.latitude+','+my_item.longitude+'&sensor=false" /></p>';
    $("#poi_details").append(output);
}



// databaseError verrà eseguita in caso di errore nella chiamata al database
function databaseError(error) {
    $("#messageError").html("Database Error");
    alert("DatabaseError");
}



$(".audio").bind("touchstart", function() {
                      alert("stai per riprodurre un brano");
                      });





/* Funzioni per "Around ME" */
function onSuccessAround(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;

    var min_lat = latitude-0.002;
    var max_lat = latitude+0.002;
    var min_lon = longitude-0.002;
    var max_lon = longitude+0.002;

    db = getDatabase();
    db.transaction(function(tx) {
        tx.executeSql("SELECT * FROM myPoi WHERE latitude BETWEEN "+min_lat+" AND "+max_lat+" AND longitude BETWEEN "+min_lon+" AND "+max_lon+"", [], querySuccessPoi, databaseError);
    }, databaseError);
}


function onErrorAround(error) {
    alert("Non posso cercare i posti vicino a te; controlla se il GPS è attivo");
}


